11. Comparing Strings

Comparing Strings

Comparing strings

Another way to work with strings is by comparing them. You've seen the comparison operators == and != when you compared numbers for equality. You can also use them with strings! For example, let’s compare the string "Yes" to "yes".

"Yes" == "yes"

Returns: false

When you run this in the console, it returns false. Why is that? "Yes" and "yes" are the same string, right? Well not quite.

Case-sensitive

When you compare strings, case matters. While both string use the same letters (and those letters appear in the same order), the first letter in the first string is a capital Y while the first letter in the second string is a lowercase y.

'Y' != 'y'

Returns: true

Which Comparisons Are True?

Enter each expression into the console. Check the ones that evaluate to true.

SOLUTION:
  • "green" == "green"
  • "green" > "blue"
  • "green" > "Green"